import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Layouts 1.15
import QtQuick.Shapes 1.15

MainView {
    id: root
    width: 800
    height: 391
    property color btnColor: "#7080C0"
    property int btnHeight: 50
    property int btnWidth: 110
    signal startLoad
    onStartLoad: {
        presetLoader.visible=true
        presetTimmer.start()
        mymodbus.getPreIsStable1()
        mymodbus.getPreMinDuration1()
        mymodbus.getPreCondition1()
        mymodbus.getPreValue11()
        mymodbus.getPreValue21()

        mymodbus.getPreIsStable2()
        mymodbus.getPreMinDuration2()
        mymodbus.getPreCondition2()
        mymodbus.getPreValue12()
        mymodbus.getPreValue22()

        mymodbus.getPreIsStable3()
        mymodbus.getPreMinDuration3()
        mymodbus.getPreCondition3()
        mymodbus.getPreValue13()
        mymodbus.getPreValue23()

        mymodbus.getPreIsStable4()
        mymodbus.getPreMinDuration4()
        mymodbus.getPreCondition4()
        mymodbus.getPreValue14()
        mymodbus.getPreValue24()
    }
    Component.onCompleted: {
        startLoad()
    }
    Rectangle{
        id:presetMainRec
        anchors.fill:parent
        color:"transparent"
    GridLayout{
        id: presetPosSettingsLayout
        rows: 4
        columns: 4
        anchors.centerIn: parent
        rowSpacing: 40
        MyComboBox
        {
            id: posIndex
            font.pixelSize: 20
            Layout.row: 0
            Layout.column: 0
            Layout.columnSpan: 2
            Layout.preferredWidth: 160
            Layout.preferredHeight: 40
            model:[qsTr("预置位1"),qsTr("预置位2"),qsTr("预置位3"),qsTr("预置位4")]
            onActivated: {
                if(posIndex.currentIndex == 1){
                    cmbIsStable.currentIndex=mymodbus.preIsStable2
                    minDuration.text=mymodbus.preMinDuration2
                    cmbCondition.currentIndex=mymodbus.preCondition2
                    value1.text=mymodbus.preValue12
                    value2.text=mymodbus.preValue22
                }
                else if(posIndex.currentIndex == 2){
                    cmbIsStable.currentIndex=mymodbus.preIsStable3
                    minDuration.text=mymodbus.preMinDuration3
                    cmbCondition.currentIndex=mymodbus.preCondition3
                    value1.text=mymodbus.preValue13
                    value2.text=mymodbus.preValue23
                }
                else if(posIndex.currentIndex == 3){
                    cmbIsStable.currentIndex=mymodbus.preIsStable4
                    minDuration.text=mymodbus.preMinDuration4
                    cmbCondition.currentIndex=mymodbus.preCondition4
                    value1.text=mymodbus.preValue14
                    value2.text=mymodbus.preValue24
                }
                else{
                    cmbIsStable.currentIndex=mymodbus.preIsStable1
                    minDuration.text=mymodbus.preMinDuration1
                    cmbCondition.currentIndex=mymodbus.preCondition1
                    value1.text=mymodbus.preValue11
                    value2.text=mymodbus.preValue21
                }
            }
        }
        Button
        {
            Layout.row:1
            Layout.column:0
            Layout.preferredWidth: btnWidth
            Layout.preferredHeight: btnHeight
            // x: 152
            // y: 13
            text: qsTr("是否判稳")
            font.pixelSize: 18
            background: Rectangle{
                color: btnColor
                    }
            onClicked: {
                cmbIsStable.popup.open()
                //miniKeyBoard.lineValue = "";
                //miniKeyBoard.textInput=oldPwd;
            }
        }
        MyComboBox
        {
            id: cmbIsStable
            font.pixelSize: 20
            Layout.row: 1
            Layout.column: 1
            Layout.columnSpan: 2
            Layout.preferredWidth: btnWidth
            Layout.preferredHeight: btnHeight
            model:[qsTr("否"),qsTr("是")]
            currentIndex: mymodbus.preIsStable1
        }
//        Rectangle {
//            id: recIsStable
//            width:300
//            height:30
//            // x: 218
//            // y: 7
//            color: "transparent"
//            border.width: 0
//            border.color: "white"
//            Layout.row:1
//            Layout.column:1
//            Layout.preferredWidth: btnWidth
//            Layout.preferredHeight: btnHeight
//            // 绘制一条边框
//            Shape {
//                anchors.fill: parent
//                ShapePath {
//                    strokeWidth: 3
//                    strokeColor: "white"
//                    strokeStyle: ShapePath.SolidLine
//                    startX: 0
//                    startY: recIsStable.height
//                    PathLine {
//                        x: recIsStable.width
//                        y: recIsStable.height
//                    }
//                }
//            }
//            TextInput{
//                id:isStable
//                anchors.fill: parent
//                echoMode: TextInput.Password
//                color:"white"
//                font.pixelSize: 25
//                horizontalAlignment: Text.AlignHCenter
//                MouseArea {
//                       anchors.fill: parent
//                       onClicked: {
//                           //miniKeyBoard.lineValue = "";
//                           //miniKeyBoard.textInput=oldPwd;
//                       }
//                   }

//            }
//        }
            Button
            {
                Layout.row:1
                Layout.column:2
                Layout.preferredWidth: btnWidth
                Layout.preferredHeight: btnHeight
                // x: 152
                // y: 13
                text: qsTr("持续时间")
                font.pixelSize: 18
                background: Rectangle{
                    color: btnColor
                        }
                onClicked: {
                    //miniKeyBoard.lineValue = "";
                    //miniKeyBoard.textInput=oldPwd;
                    //rectangle.selectedView=16
                    enterNumberPanel.lastButtonIndex = 4;
                    enterNumberPanel.lastViewIndex =10;
                    enterNumberPanel.minimum=0;
                    enterNumberPanel.clearText()
                    enterNumberPanel.maximum=999
                    enterNumberPanel.itemName=qsTr("持续时间")
                    enterNumberPanel.itemValue=minDuration.text
                    enterNumberPanel.textInput=minDuration;
                    enterNumberPanel.visible=true;
                    enterNumberPanel.lastRec=presetMainRec
                    presetMainRec.visible=false

                }
            }

            Rectangle {
                id: recMinDuration
                width:300
                height:30
                // x: 218
                // y: 7
                color: "transparent"
                border.width: 0
                border.color: "white"
                Layout.row:1
                Layout.column:3
                Layout.preferredWidth: btnWidth
                Layout.preferredHeight: btnHeight
                // 绘制一条边框
                Shape {
                    anchors.fill: parent
                    ShapePath {
                        strokeWidth: 3
                        strokeColor: "white"
                        strokeStyle: ShapePath.SolidLine
                        startX: 0
                        startY: recMinDuration.height
                        PathLine {
                            x: recMinDuration.width
                            y: recMinDuration.height
                        }
                    }
                }
                TextInput{
                    id:minDuration
                    anchors.fill: parent
                    //echoMode: TextInput.Password
                    color:"white"
                    font.pixelSize: 25
                    horizontalAlignment: Text.AlignHCenter
                    verticalAlignment: Text.AlignBottom
                    text:mymodbus.preMinDuration1
                    MouseArea {
                           anchors.fill: parent
                           onClicked: {
                               // miniKeyBoard.lineValue = "";
                               // miniKeyBoard.textInput=oldPwd;
                               //rectangle.selectedView=16
                               enterNumberPanel.lastButtonIndex = 4;
                               enterNumberPanel.lastViewIndex =10;
                               enterNumberPanel.minimum=0;
                               enterNumberPanel.clearText()
                               enterNumberPanel.maximum=999
                               enterNumberPanel.itemName=qsTr("持续时间")
                               enterNumberPanel.itemValue=minDuration.text
                               enterNumberPanel.textInput=minDuration;
                               enterNumberPanel.visible=true;
                               enterNumberPanel.lastRec=presetMainRec
                               presetMainRec.visible=false
                           }
                       }

                }
            }
    Button
    {
        Layout.row:2
        Layout.column:0
        Layout.preferredWidth: btnWidth
        Layout.preferredHeight: btnHeight
        // x: 152
        // y: 13
        text: qsTr("有效条件")
        font.pixelSize: 18
        background: Rectangle{
            color: btnColor
                }
        onClicked: {
            cmbCondition.popup.open()
            //miniKeyBoard.lineValue = "";
            //miniKeyBoard.textInput=oldPwd;
        }
    }
    MyComboBox
    {
        id: cmbCondition
        font.pixelSize: 20
        Layout.row: 2
        Layout.column: 1
        Layout.columnSpan: 2
        Layout.preferredWidth: btnWidth
        Layout.preferredHeight: btnHeight
        model:[qsTr("禁止"),"<","<=","==",">=",">","!=","_<>_","=<__>=",qsTr("外部触发")]
        currentIndex: mymodbus.preCondition1
    }
//    Rectangle {
//        id: recCondition
//        width:300
//        height:30
//        // x: 218
//        // y: 7
//        color: "transparent"
//        border.width: 0
//        border.color: "white"
//        Layout.row:2
//        Layout.column:1
//        Layout.preferredWidth: btnWidth
//        Layout.preferredHeight: btnHeight
//        // 绘制一条边框
//        Shape {
//            anchors.fill: parent
//            ShapePath {
//                strokeWidth: 3
//                strokeColor: "white"
//                strokeStyle: ShapePath.SolidLine
//                startX: 0
//                startY: recCondition.height
//                PathLine {
//                    x: recCondition.width
//                    y: recCondition.height
//                }
//            }
//        }
//        TextInput{
//            id:condition
//            anchors.fill: parent
//            echoMode: TextInput.Password
//            color:"white"
//            font.pixelSize: 25
//            horizontalAlignment: Text.AlignHCenter
//            MouseArea {
//                   anchors.fill: parent
//                   onClicked: {
//                       //miniKeyBoard.lineValue = "";
//                       //miniKeyBoard.textInput=oldPwd;
//                   }
//               }

//        }
//    }
        Button
        {
            Layout.row:2
            Layout.column:2
            Layout.preferredWidth: btnWidth
            Layout.preferredHeight: btnHeight
            // x: 152
            // y: 13
            text: qsTr("设定值1")
            font.pixelSize: 18
            background: Rectangle{
                color: btnColor
                    }
            onClicked: {
                //miniKeyBoard.lineValue = "";
                //miniKeyBoard.textInput=oldPwd;
                //rectangle.selectedView=16
                enterNumberPanel.lastButtonIndex = 4;
                enterNumberPanel.lastViewIndex =10;
                enterNumberPanel.minimum=-99999;
                enterNumberPanel.clearText()
                enterNumberPanel.maximum=99999
                enterNumberPanel.itemName=qsTr("设定值1")
                enterNumberPanel.itemValue=value1.text
                enterNumberPanel.textInput=value1;
                enterNumberPanel.visible=true;
                enterNumberPanel.lastRec=presetMainRec
                presetMainRec.visible=false
            }
        }

        Rectangle {
            id: recValue1
            width:300
            height:30
            // x: 218
            // y: 7
            color: "transparent"
            border.width: 0
            border.color: "white"
            Layout.row:2
            Layout.column:3
            Layout.preferredWidth: btnWidth
            Layout.preferredHeight: btnHeight
            // 绘制一条边框
            Shape {
                anchors.fill: parent
                ShapePath {
                    strokeWidth: 3
                    strokeColor: "white"
                    strokeStyle: ShapePath.SolidLine
                    startX: 0
                    startY: recValue1.height
                    PathLine {
                        x: recValue1.width
                        y: recValue1.height
                    }
                }
            }
            TextInput{
                id:value1
                anchors.fill: parent
                //echoMode: TextInput.Password
                color:"white"
                font.pixelSize: 25
                horizontalAlignment: Text.AlignHCenter
                verticalAlignment: Text.AlignBottom
                text:mymodbus.preValue11
                MouseArea {
                       anchors.fill: parent
                       onClicked: {
                           // miniKeyBoard.lineValue = "";
                           // miniKeyBoard.textInput=oldPwd;
                           //rectangle.selectedView=16
                           enterNumberPanel.lastButtonIndex = 4;
                           enterNumberPanel.lastViewIndex =10;
                           enterNumberPanel.minimum=-99999;
                           enterNumberPanel.clearText()
                           enterNumberPanel.maximum=99999
                           enterNumberPanel.itemName=qsTr("设定值1")
                           enterNumberPanel.itemValue=value1.text
                           enterNumberPanel.textInput=value1;
                           enterNumberPanel.visible=true;
                           enterNumberPanel.lastRec=presetMainRec
                           presetMainRec.visible=false
                       }
                   }

            }
        }
        Button
        {
            Layout.row:3
            Layout.column:0
            Layout.preferredWidth: btnWidth
            Layout.preferredHeight: btnHeight
            // x: 152
            // y: 13
            text: qsTr("设定值2")
            font.pixelSize: 18
            background: Rectangle{
                color: btnColor
                    }
            onClicked: {
                //miniKeyBoard.lineValue = "";
                //miniKeyBoard.textInput=oldPwd;
                //rectangle.selectedView=16
                enterNumberPanel.lastButtonIndex = 4;
                enterNumberPanel.lastViewIndex =10;
                enterNumberPanel.minimum=-99999;
                enterNumberPanel.clearText()
                enterNumberPanel.maximum=99999
                enterNumberPanel.itemName=qsTr("设定值2")
                enterNumberPanel.itemValue=value2.text
                enterNumberPanel.textInput=value2;
                enterNumberPanel.visible=true;
                enterNumberPanel.lastRec=presetMainRec
                presetMainRec.visible=false
            }
        }

        Rectangle {
            id: recValue2
            width:300
            height:30
            // x: 218
            // y: 7
            color: "transparent"
            border.width: 0
            border.color: "white"
            Layout.row:3
            Layout.column:1
            Layout.preferredWidth: btnWidth
            Layout.preferredHeight: btnHeight
            // 绘制一条边框
            Shape {
                anchors.fill: parent
                ShapePath {
                    strokeWidth: 3
                    strokeColor: "white"
                    strokeStyle: ShapePath.SolidLine
                    startX: 0
                    startY: recValue2.height
                    PathLine {
                        x: recValue2.width
                        y: recValue2.height
                    }
                }
            }
            TextInput{
                id:value2
                anchors.fill: parent
                //echoMode: TextInput.Password
                color:"white"
                font.pixelSize: 25
                text:mymodbus.preValue21
                horizontalAlignment: Text.AlignHCenter
                verticalAlignment: Text.AlignBottom
                MouseArea {
                       anchors.fill: parent
                       onClicked: {
                           //miniKeyBoard.lineValue = "";
                           //miniKeyBoard.textInput=oldPwd;
                           //rectangle.selectedView=16
                           enterNumberPanel.lastButtonIndex = 4;
                           enterNumberPanel.lastViewIndex =10;
                           enterNumberPanel.minimum=-99999;
                           enterNumberPanel.clearText()
                           enterNumberPanel.maximum=99999
                           enterNumberPanel.itemName=qsTr("设定值2")
                           enterNumberPanel.itemValue=value2.text
                           enterNumberPanel.textInput=value2;
                           enterNumberPanel.visible=true;
                           enterNumberPanel.lastRec=presetMainRec
                           presetMainRec.visible=false
                       }
                   }

            }
        }

}
    Button
    {
        id:btnPrePosSettingsOK
        x: 572
        y: 281
        //x: 675
        //y: 20
        width: 100
        height: 100
        background: Rectangle{
            border.width: 0
            color: "#C070B0"
                }

        text: qsTr("确定")
        font.pixelSize: 25
        palette.buttonText: "white"
        Connections {
            target: btnAdvSettingsOK
            function onClicked() {
                if(posIndex.currentIndex==0)
                {
                    presetLoader.visible=true
                    var res1=
                    mymodbus.setPrePosPara(posIndex.currentIndex, cmbIsStable.currentIndex,Number(minDuration.text),cmbCondition.currentIndex)
                    && mymodbus.setPreValue11(Number(value1.text))
                    && mymodbus.setPreValue21(Number(value2.text))
                    presetLoader.visible=false
                    if(res1){
                        prePointSaveOKDialog.title=qsTr("保存成功！");
                        prePointSaveOKDialog.open()
                        prePointSaveOKDialog.accepted.connect(function(){
                            prePointSaveOKDialog.close()
                            //rectangle.selectedView = 0
                            //bottomBar.selected=0
                        })
                    }
                    else{
                        prePointSaveOKDialog.title=qsTr("保存失败！");
                        prePointSaveOKDialog.open()
                        prePointSaveOKDialog.accepted.connect(function(){
                            prePointSaveOKDialog.close()
                        })
                    }
                }
                else if(posIndex.currentIndex==1){
                    presetLoader.visible=true
                    var res2=
                    mymodbus.setPrePosPara(posIndex.currentIndex, cmbIsStable.currentIndex,Number(minDuration.text),cmbCondition.currentIndex)
                    && mymodbus.setPreValue12(Number(value1.text))
                    && mymodbus.setPreValue22(Number(value2.text))
                    presetLoader.visible=false
                    if(res2){
                        prePointSaveOKDialog.title=qsTr("保存成功！");
                        prePointSaveOKDialog.open()
                        prePointSaveOKDialog.accepted.connect(function(){
                            prePointSaveOKDialog.close()
                            //rectangle.selectedView = 0
                            //bottomBar.selected=0
                        })
                    }
                    else{
                        prePointSaveOKDialog.title=qsTr("保存失败！");
                        prePointSaveOKDialog.open()
                        prePointSaveOKDialog.accepted.connect(function(){
                            prePointSaveOKDialog.close()
                        })
                    }
                }
                else if(posIndex.currentIndex==2){
                    presetLoader.visible=true
                    var res3 =
                    mymodbus.setPrePosPara(posIndex.currentIndex, cmbIsStable.currentIndex,Number(minDuration.text),cmbCondition.currentIndex)
                    && mymodbus.setPreValue13(Number(value1.text))
                    && mymodbus.setPreValue23(Number(value2.text))
                    presetLoader.visible=false
                    if(res3){
                        prePointSaveOKDialog.title=qsTr("保存成功！");
                        prePointSaveOKDialog.open()
                        prePointSaveOKDialog.accepted.connect(function(){
                            prePointSaveOKDialog.close()
                            //rectangle.selectedView = 0
                            //bottomBar.selected=0
                        })
                    }
                    else{
                        prePointSaveOKDialog.title=qsTr("保存失败！");
                        prePointSaveOKDialog.open()
                        prePointSaveOKDialog.accepted.connect(function(){
                            prePointSaveOKDialog.close()
                        })
                    }
                }
                else if(posIndex.currentIndex==3){
                    presetLoader.visible=true
                    var res4 =
                    mymodbus.setPrePosPara(posIndex.currentIndex, cmbIsStable.currentIndex,Number(minDuration.text),cmbCondition.currentIndex)
                    && mymodbus.setPreValue14(Number(value1.text))
                    && mymodbus.setPreValue24(Number(value2.text))
                    presetLoader.visible=false
                    if(res4){
                        prePointSaveOKDialog.title=qsTr("保存成功！");
                        prePointSaveOKDialog.open()
                        prePointSaveOKDialog.accepted.connect(function(){
                            prePointSaveOKDialog.close()
                            //rectangle.selectedView = 0
                            //bottomBar.selected=0
                        })
                    }
                    else{
                        prePointSaveOKDialog.title=qsTr("保存失败！");
                        prePointSaveOKDialog.open()
                        prePointSaveOKDialog.accepted.connect(function(){
                            prePointSaveOKDialog.close()
                        })
                    }
                }

            }
        }
    }
    Button
    {
        id:btnReturn
        x: 688
        y: 281
        //x: 675
        //y: 20
        width: 100
        height: 100
        background: Rectangle{
            border.width: 0
            color: "#B0B0D0"
                }

        text: qsTr("返回")
        font.pixelSize: 25
        palette.buttonText: "white"
        //Connections {
        //    target: btnReturn
            onClicked: {
                //rectangle.selectedView = 4
                bottomBar.selected=4
//                myLoader.visible=false;
//                myLoader.visible=true
                myLoader.source="SettingsPanel.qml"
            }

        //}
}

    }
    Timer{
       id:presetTimmer
       interval: 1200
       running: false
       repeat: false
       onTriggered: presetLoader.visible = false
   }

    EnterNumberPanel{
          id:enterNumberPanel
          width: 800
          height: 391
          visible:false
    }
   MyBuzyIndicator{
       anchors.centerIn: parent
       id: presetLoader
       visible:true
       z:101
   }
    Dialog {
        anchors.centerIn: parent
        id: prePointSaveOKDialog
        //title: "提示"
        standardButtons: Dialog.Ok
        //title:qsTr("保存成功！")
        modal: true
        closePolicy: "CloseOnEscape"
        //onAccepted: holdSaveOKDialog.close()
        z:100
        //onRejected: console.log("Cancel clicked")
    }
}
